package com.bjbn.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.bjbn.service.BaService;
import com.bjbn.util.HutoolTokenCacheUtil;
import com.bjbn.util.SSLUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BaServiceImpl implements BaService {

    @Autowired
    private HutoolTokenCacheUtil tokenCacheUtil;

    @Value("${ba-w-config.ba-base-url}")
    private String baBaseUrl4W;

    @Value("${ba-e-config.ba-base-url}")
    private String baBaseUrl4E;

    @Value("${ba-t3-config.ba-base-url}")
    private String baBaseUrl4T3;

    @Value("${ba-w-config.username}")
    private String username4W;

    @Value("${ba-w-config.password}")
    private String password4W;

    @Value("${ba-e-config.username}")
    private String username4E;

    @Value("${ba-e-config.password}")
    private String password4E;

    @Value("${ba-t3-config.username}")
    private String username4T3;

    @Value("${ba-t3-config.password}")
    private String password4T3;


    @Override
    public String getBaToken(String position) {
        if (tokenCacheUtil.containsToken("BA_TOKEN_" + position)) {
            return tokenCacheUtil.getToken("BA_TOKEN_" + position);
        }

        String username;
        String password;
        String baseUrl;
        String tokenUrl = "/API/api/token";
        if ("WEST".equals(position)) {
            // 读取西四的配置项
            username = username4W;
            password = password4W;
            baseUrl = baBaseUrl4W;
        } else if ("EAST".equals(position)) {
            username = username4E;
            password = password4E;
            baseUrl = baBaseUrl4E;
        } else {
            username = username4T3;
            password = password4T3;
            baseUrl = baBaseUrl4T3;
        }

        String url = baseUrl + tokenUrl;

        String grantType = "password";
        HttpRequest request = HttpUtil
                .createPost(url)
                .setSSLSocketFactory(SSLUtils.getSSLSocketFactory())
                .setConnectionTimeout(5000)
                .timeout(10000)
                .form("grant_type", grantType)
                .form("username", username)
                .form("password", password);
        // 发送POST请求并获取响应
        HttpResponse response = request.execute();
        // 打印响应状态码和响应体
        System.out.println("Response Status: " + response.getStatus());
        System.out.println("Response Body: " + response.body());
        JSONObject token = JSONObject.parseObject(response.body());

        String accessToken = token.getString("access_token");
        if (accessToken == null) {
            log.error("获取access_token失败");
        }

        System.out.println("access_token:" + accessToken);

        // 缓存token信息
        tokenCacheUtil.putToken("BA_TOKEN_" + position, accessToken);

        return accessToken;

    }

    @Override
    public String refreshBaToken(String position) {
        if (tokenCacheUtil.containsToken("BA_TOKEN_" + position)) {
            tokenCacheUtil.removeToken("BA_TOKEN_" + position);
        }
        return getBaToken(position);
    }
}
